﻿namespace NuGetCop.MSBuild.Tasks.Foundation
{
    using System;
    using System.Diagnostics.Contracts;

    internal static class ObjectExtensions
    {
        public static TResult Eval<T, TResult>(
            this T source,
            Func<T, TResult> evaluator,
            TResult defaultValue = default(TResult))
            where T : class
        {
            Contract.Requires(evaluator != null);

            var result = source == null
                ? defaultValue
                : evaluator(source);

            return result;
        }

        public static T AdjustBy<T>(this T source, Action<T> action)
        {
            Contract.Requires(source != null);
            Contract.Requires(action != null);

            action(source);

            return source;
        }
    }
}